
(function (token) {
    var _emit = console.dirxml
    var emit = function (name, value) {
        if (value === undefined) {
            return
        }
        _emit(JSON.stringify({ token: token, name: name, value: value, stacks: getstack(), url: window.location.href }))
    }
    var getstack = function () {
        var stacks = new Error().stack.split('\n').slice(3)
        var r = []
        for (s of stacks) {
            r.push(s.trim().split(" ").slice(1))
        }
        return r
    }
    // title change
    var e = null
    var timer = setInterval(() => {
        if (e) {
            clearInterval(timer)
            return
        }
        e = document.querySelector('title')
        if (!e) {
            return
        }
        emit('titlechange', e.textContent)
        if (e) {
            new MutationObserver(function (mutations) {
                emit('titlechange', mutations[0].target.textContent)
            }).observe(
                e,
                { subtree: true, characterData: true, childList: true }
            );
        }
        // change a target to _self
        new MutationObserver(function (mutations) {
            document.querySelectorAll('a[target="_blank"]').forEach(a => {
                a.target = '_self'
            })
        }).observe(
            document.querySelector('html'),
            { subtree: true, characterData: false, childList: true }
        );
    }, 1);

    // hook write
    var oldwrite = document.write
    document.write = function (markup) {
        emit('write', markup)
        oldwrite.apply(this, arguments)
    }
    document.write.toString = oldwrite.toString

    // hook writeln
    var oldwriteln = document.writeln
    document.writeln = function (markup) {
        emit('writeln', markup)
        oldwriteln.apply(this, arguments)
    }
    document.writeln.toString = oldwriteln.toString

    // hook eval
    var oldeval = window.eval
    window.eval = function (script) {
        emit('eval', script)
        oldeval.apply(this, arguments)
    }
    window.eval.toString = oldeval.toString

    // navigation hook
    window.addEventListener("beforeunload", (event) => {
        event.preventDefault()
        event.returnValue = self.token
        return event.returnValue
    });
    navigation.addEventListener("navigate", (event) => {
        emit('navigate', event.destination.url)
    })

    // window.open
    var oldopen = window.open
    window.open = function () {
        if (arguments.length >= 2) {
            arguments[1] = '_self'
            emit('open', arguments[0])
        }
        oldopen.apply(this, arguments)
    }
    window.eval.toString = oldopen.toString
    // a create hook
    // var link = document.createElement('a')
    // link.target = '_blank'
    // link.href = "https://bwshen.blog.csdn.net/"
    // link.click()
    document.createElement = new Proxy(document.createElement, {
        apply: (target, thisArg, argArray) => {
            var e = target.apply(thisArg, argArray)
            if (e.tagName == 'A') {
                var ee = new Proxy(e, {
                    get: function (target, key, receiver) {
                        if (!!target[key] && !!target[key].bind) {
                            var attr = target[key].bind(target);
                            if (key == "click") {
                                attr = function () {
                                    emit('a_click', target['href'])
                                }
                            }
                            return attr

                        } else {
                            return target[key];
                        }
                    }
                })
                return ee
            }

            return e
        }
    })

})("{{TOKEN}}")
